//
//  HttpServiceWrapper.m
//  Poem
//
//  Created by 高波 on 2017/7/19.
//  Copyright © 2017年 tianwan. All rights reserved.
//

#import "HttpServiceWrapper.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
@implementation HttpServiceWrapper

+ (HttpServiceWrapper *)instance {
    static dispatch_once_t onceToken;
    static HttpServiceWrapper *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
  
    return instance;
}

- (void)GET:(NSString*)url parameters:(id)params session:(NSString *)session resultBlock:(ResponseBlock)block withDialog:(BOOL)show
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        block(YES, responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        block(NO, error);
        
    }];
}

- (void)POST:(NSString*)url parameters:(id)params session:(NSString *)session resultBlock:(ResponseBlock)block withDialog:(BOOL)show
{
    if (show) {
        [SVProgressHUD show];
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (session.length) {
        [manager.requestSerializer setValue:session forHTTPHeaderField:@"ticket"];
    }

    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    // request
    NSError *requestError = nil;
    NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:url parameters:nil error:&requestError];
    
    // body
    if (params) {
        NSData *postData = [params dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:postData];
    }
    
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"app/json",nil];
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        NSLog(@"-----responseObject===%@+++++",responseObject);
        
        if (show) {
            [SVProgressHUD dismiss];
        }
        
        if (!error) {
            block(YES, responseObject);
        } else {
            block(NO, error);
        }
    }];
    
    [task resume];
    
    
    
    
    
//    [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@-%@",url,responseObject);
//        block(YES, responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//        block(NO, error);
//
//    }];
}

@end
